<template>
  <view style="height: 100vh;background-color: #f1f1f1;padding: 20rpx;">
    <!-- /上方的收货地址 -->
    <view class="top" @click="adress">
      <!-- payObj.user_address -->
      <view v-if="dizhiid != 0" style="padding: 20rpx ;display: flex;align-items:center;justify-content: space-between;">
        <view>
          <view class="address">{{ payObj.user_address.consignee }} {{ payObj.user_address.phone }}</view>
          <view class="contacts">
            {{ payObj.user_address.province_name }} {{ payObj.user_address.city_name }} {{ payObj.user_address.area_name }} {{ payObj.user_address.address }}
          </view>
        </view>
        <view style="text-align: right;"><image style="width: 48rpx;height: 48rpx;" src="/static/lzl/jiantou (1).png" mode="widthFix"></image></view>
      </view>
      <view v-else style="padding: 20rpx ;display: flex;align-items:center;justify-content: space-between;">
        <view><view class="address">请选择收货地址</view></view>
        <view style="text-align: right;"><image style="width: 48rpx;height: 48rpx;" src="/static/lzl/jiantou (1).png" mode="widthFix"></image></view>
      </view>
    </view>
    <!-- 商品详情 -->
    <view class="shopping" v-for="(item, index) in payObj.new_goods_list" :key="index">
      <view class="left"><image style="width: 140rpx;height: 140rpx;" :src="item.detail.image" mode="widthFix"></image></view>
      <view class="center">
        <view class="tit">{{ item.detail.title }}</view>
        <view style="font-weight: 400;font-size: 24rpx;color: #aaa;margin-top: 20rpx; ">
          {{ item.detail.current_sku_price.goods_sku_text ? item.detail.current_sku_price.goods_sku_text : '' }}
        </view>
        <!-- <view style="font-weight: 400;font-size: 26rpx;font-weight: 600; color: #ff0000;margin-top: 0rpx; ">总价：{{ item.pay_price }}</view> -->
      </view>
      <view class="right">
        <view class="">￥{{ item.goods_price }}</view>
        <view style="text-align: right;font-weight: 400;color: #aaa;">x{{ item.goods_num }}</view>
      </view>
    </view>
    <!-- 支付方式 -->
    <view style="background-color: #fff;margin-bottom: 20rpx;">
      <view @click="payMethod" style=" padding: 20rpx ;display: flex;align-items:center;justify-content: space-between;">
        <view><view class="address" style="font-size: 30rpx;">支付方式</view></view>
        <view style="text-align: right;color: #aaa;">
          {{ payType == 'wallet' ? '余额支付' : '微信支付' }}
          <image style="width: 48rpx;height: 48rpx;vertical-align: middle;" src="/static/lzl/jiantou (1).png" mode="widthFix"></image>
        </view>
      </view>
      <view @click="coupon" style=" padding: 20rpx ;display: flex;align-items:center;justify-content: space-between;">
        <view><view class="address" style="font-size: 30rpx;">优惠卷</view></view>
        <view style="text-align: right;color: #aaa;">
          {{ coupons_id == 0 ? couponPri : youhuihou }}
          <image style="width: 48rpx;height: 48rpx;vertical-align: middle;" src="/static/lzl/jiantou (1).png" mode="widthFix"></image>
        </view>
      </view>
    </view>
    <!-- 订单备注 -->
    <view style="background-color: #fff;margin-bottom: 20rpx;padding-bottom: 100rpx;">
      <view style=" padding: 20rpx ;display: flex;align-items:center;justify-content: space-between;">
        <view><view class="address" style="font-size: 30rpx;">商品总额</view></view>
        <view style="text-align: right;">{{ payObj.total_amount }}</view>
      </view>
      <view style=" padding: 20rpx ;display: flex;align-items:center;justify-content: space-between;">
        <view><view class="address" style="font-size: 30rpx;">运费</view></view>
        <view style="text-align: right;">￥{{ payObj.dispatch_amount }}</view>
      </view>
      <view style=" padding: 20rpx ;display: flex;align-items:center;justify-content: space-between;">
        <view><view class="address" style="font-size: 30rpx;">订单备注</view></view>
        <view style="text-align: right;"><input v-model="text" type="text" style="text-align: right;" placeholder="请输入备注信息" name="" id="" /></view>
      </view>
    </view>
    <view class="botton">
      <view class="left">
        <view style="font-size: 28rpx;color: #aaa;margin-right: 10rpx;">共1件</view>
        <view style="font-size: 32rpx;color: #000;margin-right: 10rpx;">合计：</view>
        <view style="font-size: 32rpx;color: #ff0000;">￥{{ payObj.total_fee }}</view>
      </view>
      <view class="right" @click="orderGood">提交订单</view>
    </view>
    <!-- 选择支付方式 -->
    <u-popup v-model="showPaly" mode="bottom" :mask-close-able="true" border-radius="20">
      <!-- 下方的支付方式 -->
      <view style="padding: 20rpx;">
        <view class="payOrder" @click="xiaoshi">
          <view class="left">
            <image style="width: 64rpx;height: 64rpx;vertical-align: middle;margin-right: 10rpx;" src="/static/lzl/yue@2x.png" mode="widthFix"></image>
            余额支付
          </view>
          <view>
            <radio-group style="margin-top: 10rpx;" @change="checkChang"><radio @change="checkChang" :checked="current" /></radio-group>
          </view>
        </view>
        <view class="payOrder" @click="xiaoshi">
          <view class="left">
            <image style="width: 64rpx;height: 64rpx;vertical-align: middle;margin-right: 10rpx;" src="/static/lzl/weixin@2x.png" mode="widthFix"></image>
            微信支付
          </view>
          <view>
            <radio-group style="margin-top: 10rpx;" @change="checkChang"><radio @change="checkChang" :checked="!current" /></radio-group>
          </view>
        </view>
      </view>
    </u-popup>
    <!-- 选择优惠卷 -->
    <u-popup height="250px" v-model="couponPlay" mode="bottom" :mask-close-able="true" border-radius="20">
      <!--     <view style="padding: 20rpx;">
        <view style="text-align: center; font-size: 32rpx;margin-bottom: 20rpx;" @click="coupons(item)" v-for="(item, index) in 6">
          点我使用优惠卷 优惠
          <text style="font-size: 34rpx;color: coral;font-weight: 600;">满6</text>
          优惠
          <text style="font-size: 34rpx;color: coral;font-weight: 600;">5</text>
          元
        </view>
      </view> -->
      <view @click="noCoupon" style="font-size: 32rpx;color: #ff0000;margin-right: 20rpx; text-align: right;margin-top: 20rpx;margin-bottom: 20rpx;">不使用优惠卷</view>
      <view style="margin-top: 150rpx;margin-left: 50%;transform: translateX(-35%);font-size: 32rpx;" v-if="couponsList == 0">暂时还没有优惠卷</view>
      <view class="card1" v-for="item in couponsList" :key="item.id">
        <view class="card-right">
          <view class="left">
            {{ item.amount }}
            <span>元</span>
          </view>
          <view class="right">
            <p class="card-info">满{{ item.enough }}减{{ item.amount }}</p>
            <strong class="card-time">有效期至：2023-7-15</strong>
          </view>
        </view>
        <view class="card-left" @click="coupons(item.user_coupons_id)">使用</view>
      </view>
    </u-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      text: '',
      dizhiid: '',
      couponPri: '选择优惠卷', //优惠卷文字提示
      youhuihou: '',
      couponPlay: false,
      showPaly: false,
      current: false,
      payType: 'wechat',
      defaults: {}, //默认地址
      obj: {},
      payObj: {},
      couponsList: [],
      coupons_id: '' //优惠卷id
    };
  },
  onShow() {
    this.dizhiid = uni.getStorageSync('address_id');
    // 如果obj存在再执行一次
    if (this.obj) {
      let dizhi = uni.getStorageSync('address_id');
      this.preObj(dizhi, 0, this.obj);
    }
  },
  onLoad(option) {
    // 获取默认地址
    this.defaultsDeess();
    let obj = JSON.parse(option.obj);
    this.obj = obj;
    let dizhi = uni.getStorageSync('address_id');
    this.preObj(dizhi, 0, this.obj);
    // 获取优惠卷
    this.couponsDet(this.obj);
  },
  methods: {
    // 点击支付
    async orderGood() {
      if (this.dizhiid == 0) {
        return this.$msg('请选择收货地址');
      }
      console.log(this.dizhiid);
      let n = await this.$api.createOrder({
        ...this.obj,
        address_id: this.dizhiid,
        coupons_id: this.coupons_id,
        invoice: {},
        remark: this.text
      });
      if (this.payType == 'wechat') {
        // 微信支付
        let res = await this.$api.prepay({
          order_sn: n.data.order_sn,
          payment: this.payType,
          return_url: '/pages/payResult/payResult'
        });
        console.log(res);
        if (res.code == 1) {
          wx.requestPayment({
            //成功之后，调用小程序微信支付
            timeStamp: res.data.pay_data.timeStamp,
            nonceStr: res.data.pay_data.nonceStr,
            package: res.data.pay_data.package,
            signType: 'MD5',
            paySign: res.data.pay_data.paySign,
            success: res => {
              console.log(res);
              //支付成功的回调
              // console.log('支付成功', JSON.stringify(res));
              // this.show = true;
              uni.navigateTo({
                url: '/pages/payResult/payResult'
              });
            },
            fail: () => {
              // uni.showToast({
              //   icon: 'none',
              //   title: '支付失败'
              // });
            }
          });
        }
      } else {
        // 余额支付
        let m = await this.$api.prepay({
          order_sn: n.data.order_sn,
          payment: this.payType,
          return_url: '/pages/payResult/payResult'
        });
        console.log(m);
        // this.$msg(m.msg);
        if (m.code == 1) {
          uni.navigateTo({
            url: '/pages/payResult/payResult'
          });
        }
        console.log(m);
      }
    },
    // 点击不使用优惠卷
    noCoupon() {
      this.couponPlay = false;
      this.coupons_id = 0;
    },
    // 点击去选择地址-------------------------------------------------------------------------------------------------------------------------------------------------
    adress() {
      uni.navigateTo({
        url: '/pages/address/address'
      });
    },
    // 点击支付方式
    payMethod() {
      this.showPaly = true;
    },
    // 点击切换支付方式
    checkChang() {
      this.current = !this.current;
      if (this.current) {
        this.payType = 'wallet';
      } else {
        this.payType = 'wechat';
      }
    },
    // 点击之后消失
    xiaoshi() {
      this.showPaly = false;
    },
    // 点击使用优惠卷
    coupons(id) {
      this.couponPlay = false;
      this.coupons_id = id;
      let dizhi = uni.getStorageSync('address_id');
      this.preObj(dizhi, id, this.obj);
    },
    // 点击优惠卷
    coupon() {
      this.couponPlay = true;
    },
    // 获取默认地址
    async defaultsDeess() {
      let n = await this.$api.defaults();
      if (n.data) {
        uni.setStorageSync('address_id', n.data.id);
        this.defaults = n.data;
      } else {
        uni.setStorageSync('address_id', 0);
      }
    },
    // 获取优惠卷信息
    async couponsDet(data) {
      let n = await this.$api.coupons({ ...data, address_id: 0, coupons_id: 0, order_type: 'goods' });
      this.couponsList = n.data;
    },
    // 获取订单详情
    async preObj(address_id = 0, coupons_id = 0, data) {
      let n = await this.$api.pre({ address_id: address_id, coupons_id: coupons_id, ...data });
      this.payObj = n.data;
      if (n.data.discount_fee != '0.00') {
        this.youhuihou = '已优惠' + n.data.discount_fee;
      }
    }
  }
};
</script>

<style lang="scss">
// 下方的
.botton {
  z-index: 9999;
  position: fixed;
  height: 100rpx;
  padding: 20rpx;
  width: 95%;
  bottom: 0;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .left {
    display: flex;
  }
  .right {
    padding: 20rpx 40rpx;
    border-radius: 50rpx;
    height: 80rpx;
    background-color: #eabb6d;
    color: #fff;
    font-size: 32rpx;
  }
}
.card1 {
  margin: 32rpx 0;
  display: flex;
  align-items: center;
  width: 100%;
  height: 148rpx;
  padding: 10px 20rpx;
  // background: #f0f0f0;
  background: #d24c39;
  border-radius: 4rpx;
  -webkit-mask-image: radial-gradient(circle at 460rpx 8rpx, transparent 8rpx, red 18rpx), radial-gradient(closest-side circle at 50%, red 99%, transparent 100%);
  -webkit-mask-size: 100%, 4rpx 8rpx;
  -webkit-mask-repeat: repeat, repeat-y;
  -webkit-mask-position: 0 -4px, 460rpx;
  -webkit-mask-composite: source-out;
  mask-composite: subtract;
  box-shadow: 0rpx 8rpx 36rpx 0rpx rgba(188, 188, 188, 1);
  // background: linear-gradient(45deg, orange, red);

  .card-right {
    padding: 22rpx 32rpx 22rpx;
    display: flex;
    // flex: 1;
    // flex-direction: column;
    .left {
      width: 150rpx;
      font-size: 30rpx;
      font-family: Source Han Sans SC-Bold, Source Han Sans SC;
      font-weight: bold;
      color: #fff;
      line-height: 84rpx;
      margin-right: 10rpx;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      span {
        font-size: 30rpx;
      }
    }
    .right {
      margin-top: 10rpx;
      .card-info {
        font-size: 28rpx;
        font-family: Source Han Sans SC-Regular, Source Han Sans SC;
        font-weight: 400;
        color: #fff;
      }
      .card-time {
        font-size: 24rpx;
        font-family: Source Han Sans SC-Regular, Source Han Sans SC;
        font-weight: 400;
        color: #fff;
      }
    }
  }
  .card-left {
    width: 144rpx;
    height: 64rpx;
    border-radius: 72rpx 72rpx 72rpx 72rpx;
    opacity: 1;
    border: 2rpx solid #fff;
    font-size: 28rpx;
    font-family: Source Han Sans SC-Regular, Source Han Sans SC;
    font-weight: 400;
    color: #fff;
    line-height: 64rpx;
    text-align: center;
    margin: 0 auto;
  }
}
.payOrder {
  display: flex;
  margin-top: 20rpx;
  justify-content: space-between;
  margin-bottom: 30rpx;
}
.shopping {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 20rpx;
  margin: 10rpx 0;
  box-sizing: border-box;
  .center {
    width: 360rpx;
    box-sizing: border-box;
    padding-left: 10rpx;
    font-size: 32rpx;
    font-weight: 600;
    color: #000;
    .tit {
      display: -webkit-box; //对象作为弹性伸缩盒子模型显示
      overflow: hidden; //溢出隐藏
      -webkit-box-orient: vertical; //设置伸缩盒子对象的子元素的排列方式
      -webkit-line-clamp: 2; //设置 块元素包含的文本行数
    }
  }
  .right {
    font-size: 24rpx;
    font-weight: 600;
  }
}
.top {
  background-color: #fff;
}
.address {
  font-size: 32rpx;
  color: #000;
  font-weight: 600;
  display: -webkit-box; //对象作为弹性伸缩盒子模型显示
  overflow: hidden; //溢出隐藏
  -webkit-box-orient: vertical; //设置伸缩盒子对象的子元素的排列方式
  -webkit-line-clamp: 1; //设置 块元素包含的文本行数
}
.contacts {
  font-size: 24rpx;
  color: #aaa;
  display: -webkit-box; //对象作为弹性伸缩盒子模型显示
  overflow: hidden; //溢出隐藏
  -webkit-box-orient: vertical; //设置伸缩盒子对象的子元素的排列方式
  -webkit-line-clamp: 1; //设置 块元素包含的文本行数
}
</style>
